/* TLS test - check appropriate behavior of TLS Client if no local certificate is assigned and the server requests a
 * Client Certificate message. The Client should send a Certificate message with an empty data field (0 bytes length)
 * and the Server then chooses what to do with the connection. Default for NetX Secure TLS Server is to reject
 * the handshake when no certificate is provided by the client.
 */


#include   "tx_api.h"
#include   "nx_api.h"
#include   "nx_tcp.h"
#include   "nx_secure_tls_api.h"
#include   "tls_test_utility.h"

extern void    test_control_return(UINT status);

/* Specific certificate test - don't run if PSK enabled! */
#if !defined(NX_SECURE_TLS_CLIENT_DISABLED) && !defined(NX_SECURE_TLS_SERVER_DISABLED) && defined(NX_SECURE_ENABLE_CLIENT_CERTIFICATE_VERIFY) && !defined(NX_SECURE_ENABLE_PSK_CIPHERSUITES)
#define __LINUX__

#define MSG "----------abcdefgh20----------ABCDEFGH40----------klmnopqr60----------KLMNOPQR80--------------------"

/* Define the ThreadX and NetX object control blocks...  */

static TX_THREAD               ntest_0;
static TX_THREAD               ntest_1;

static NX_PACKET_POOL          pool_0;
static NX_IP                   ip_0;
static NX_IP                   ip_1;
static NX_TCP_SOCKET           client_socket;
static NX_TCP_SOCKET           server_socket;
static NX_SECURE_TLS_SESSION   client_tls_session;
static NX_SECURE_TLS_SESSION   server_tls_session;

static NX_SECURE_X509_CERT certificate;
static NX_SECURE_X509_CERT ica_certificate;
static NX_SECURE_X509_CERT client_certificate;
static NX_SECURE_X509_CERT remote_certificate, remote_issuer;
static NX_SECURE_X509_CERT client_remote_certificate, client_remote_issuer;
static NX_SECURE_X509_CERT trusted_certificate;

UCHAR remote_cert_buffer[2000];
UCHAR remote_issuer_buffer[2000];
UCHAR client_remote_cert_buffer[2000];
UCHAR client_remote_issuer_buffer[2000];

UCHAR server_packet_buffer[4000];
UCHAR client_packet_buffer[4000];

CHAR server_crypto_metadata[16000]; 
CHAR client_crypto_metadata[16000]; 

/* Test PKI (3-level). */
#include "test_ca_cert.c"
#define ca_cert_der test_ca_cert_der
#define ca_cert_der_len test_ca_cert_der_len

/*  Cryptographic routines. */
extern const NX_SECURE_TLS_CRYPTO nx_crypto_tls_ciphers;

static unsigned char ica_cert_der[] = {
  0x30, 0x82, 0x03, 0xdf, 0x30, 0x82, 0x02, 0xc7, 0xa0, 0x03, 0x02, 0x01,
  0x02, 0x02, 0x01, 0x02, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x7a, 0x31, 0x0b, 0x30,
  0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x0b,
  0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x02, 0x43, 0x41, 0x31,
  0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x09, 0x53, 0x61,
  0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x31, 0x16, 0x30, 0x14, 0x06,
  0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0d, 0x45, 0x78, 0x70, 0x72, 0x65, 0x73,
  0x73, 0x20, 0x4c, 0x6f, 0x67, 0x69, 0x63, 0x31, 0x14, 0x30, 0x12, 0x06,
  0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x4e, 0x65, 0x74, 0x58, 0x20, 0x53,
  0x65, 0x63, 0x75, 0x72, 0x65, 0x31, 0x1c, 0x30, 0x1a, 0x06, 0x03, 0x55,
  0x04, 0x03, 0x0c, 0x13, 0x4e, 0x65, 0x74, 0x58, 0x20, 0x53, 0x65, 0x63,
  0x75, 0x72, 0x65, 0x20, 0x54, 0x65, 0x73, 0x74, 0x20, 0x43, 0x41, 0x30,
  0x1e, 0x17, 0x0d, 0x31, 0x37, 0x30, 0x33, 0x33, 0x31, 0x32, 0x31, 0x30,
  0x30, 0x30, 0x32, 0x5a, 0x17, 0x0d, 0x32, 0x37, 0x30, 0x33, 0x32, 0x39,
  0x32, 0x31, 0x30, 0x30, 0x30, 0x32, 0x5a, 0x30, 0x6f, 0x31, 0x0b, 0x30,
  0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x0b,
  0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x02, 0x43, 0x41, 0x31,
  0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0d, 0x45, 0x78,
  0x70, 0x72, 0x65, 0x73, 0x73, 0x20, 0x4c, 0x6f, 0x67, 0x69, 0x63, 0x31,
  0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x09, 0x4e, 0x58,
  0x20, 0x53, 0x65, 0x63, 0x75, 0x72, 0x65, 0x31, 0x27, 0x30, 0x25, 0x06,
  0x03, 0x55, 0x04, 0x03, 0x0c, 0x1e, 0x4e, 0x58, 0x20, 0x53, 0x65, 0x63,
  0x75, 0x72, 0x65, 0x20, 0x54, 0x65, 0x73, 0x74, 0x20, 0x49, 0x6e, 0x74,
  0x65, 0x72, 0x6d, 0x65, 0x64, 0x69, 0x61, 0x74, 0x65, 0x20, 0x43, 0x41,
  0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
  0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xa7, 0xa4, 0xd9,
  0xd0, 0x19, 0x84, 0x4e, 0xab, 0x8e, 0x08, 0xad, 0x30, 0xe6, 0x79, 0x46,
  0x46, 0xe2, 0xd3, 0x13, 0xc1, 0x69, 0xa9, 0x4a, 0xc9, 0xc8, 0x95, 0x54,
  0xa8, 0x76, 0x41, 0x63, 0x10, 0xc4, 0xe4, 0xfd, 0x08, 0x69, 0x7f, 0x89,
  0x79, 0x82, 0x62, 0xd7, 0x90, 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9, 0x96, 0xff, 0x46, 0xac, 0xdd,
  0x44, 0x9f, 0x3a, 0xc8, 0x83, 0xeb, 0x1b, 0x67, 0xa0, 0x2d, 0xa8, 0x34,
  0x19, 0xf0, 0x5b, 0xe8, 0x59, 0xf7, 0xc9, 0x08, 0xab, 0x09, 0x71, 0x78,
  0x75, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x7b, 0x30, 0x79, 0x30, 0x09,
  0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x2c, 0x06,
  0x09, 0x60, 0x86, 0x48, 0x01, 0x86, 0xf8, 0x42, 0x01, 0x0d, 0x04, 0x1f,
  0x16, 0x1d, 0x4f, 0x70, 0x65, 0x6e, 0x53, 0x53, 0x4c, 0x20, 0x47, 0x65,
  0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x64, 0x20, 0x43, 0x65, 0x72, 0x74,
  0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x30, 0x1d, 0x06, 0x03, 0x55,
  0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x8e, 0x6a, 0xb0, 0xfe, 0x49, 0xc5,
  0x6a, 0x96, 0x9e, 0xba, 0xb8, 0xb6, 0x58, 0xfc, 0x52, 0x7f, 0xab, 0x51,
  0xf2, 0x3a, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30,
  0x16, 0x80, 0x14, 0x1b, 0x8d, 0x06, 0xd9, 0x6b, 0xad, 0xee, 0x82, 0x24,
  0x26, 0x55, 0x9a, 0x1b, 0x03, 0x44, 0x92, 0x0a, 0x06, 0x92, 0x48, 0x30,
  0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x18, 0x5a, 0x50, 0xb6, 0x44,
  0xba, 0x68, 0xac, 0x7f, 0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, 0x90, 0x20, 0xac, 0xb3, 0x99,
  0x61, 0xa0, 0xda, 0xe5, 0x8d, 0x3f, 0x66, 0xd1, 0xf3, 0x91, 0x40, 0x60,
  0xcf, 0x97, 0xbb, 0x15, 0xb1, 0x81, 0x4e, 0xbf, 0xba, 0xef, 0x20, 0x36,
  0x6f, 0x00, 0x40, 0xa2, 0xa3, 0x24, 0x5c, 0x61, 0xd5, 0xa5, 0x64, 0xbf,
  0xbe, 0x29, 0xdf, 0x47, 0x4b, 0xc8, 0x76, 0x4f, 0x2e, 0x4c, 0x5c, 0xed,
  0x99, 0x26, 0xe8, 0xf1, 0x72, 0x5e, 0x9b, 0x86, 0x73, 0xda, 0xfb
};
static unsigned int ica_cert_der_len = 995;

static unsigned char test_device_cert_der[] = {
  0x30, 0x82, 0x03, 0xdb, 0x30, 0x82, 0x02, 0xc3, 0xa0, 0x03, 0x02, 0x01,
  0x02, 0x02, 0x02, 0x10, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x6f, 0x31, 0x0b,
  0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x02, 0x43, 0x41,
  0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0d, 0x45,
  0x78, 0x70, 0x72, 0x65, 0x73, 0x73, 0x20, 0x4c, 0x6f, 0x67, 0x69, 0x63,
  0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x09, 0x4e,
  0x58, 0x20, 0x53, 0x65, 0x63, 0x75, 0x72, 0x65, 0x31, 0x27, 0x30, 0x25,
  0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x1e, 0x4e, 0x58, 0x20, 0x53, 0x65,
  0x63, 0x75, 0x72, 0x65, 0x20, 0x54, 0x65, 0x73, 0x74, 0x20, 0x49, 0x6e,
  0x74, 0x65, 0x72, 0x6d, 0x65, 0x64, 0x69, 0x61, 0x74, 0x65, 0x20, 0x43,
  0x41, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x37, 0x30, 0x33, 0x33, 0x31, 0x32,
  0x31, 0x30, 0x39, 0x30, 0x39, 0x5a, 0x17, 0x0d, 0x32, 0x37, 0x30, 0x33,
  0x32, 0x39, 0x32, 0x31, 0x30, 0x39, 0x30, 0x39, 0x5a, 0x30, 0x75, 0x31,
  0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x43,
  0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61, 0x31, 0x16, 0x30,
  0x14, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0d, 0x45, 0x78, 0x70, 0x72,
  0x65, 0x73, 0x73, 0x20, 0x4c, 0x6f, 0x67, 0x69, 0x63, 0x31, 0x12, 0x30,
  0x10, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x09, 0x4e, 0x58, 0x20, 0x53,
  0x65, 0x63, 0x75, 0x72, 0x65, 0x31, 0x25, 0x30, 0x23, 0x06, 0x03, 0x55,
  0x04, 0x03, 0x0c, 0x1c, 0x4e, 0x58, 0x20, 0x53, 0x65, 0x63, 0x75, 0x72,
  0x65, 0x20, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x20, 0x43, 0x65, 0x72,
  0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x30, 0x82, 0x01, 0x22,
  0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  0x01, 0x05, 0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 0xd0, 0x7e, 0x1d, 0x5e, 0x9b, 0xf7, 0xf3,
  0x5b, 0xd4, 0xf5, 0xde, 0xa5, 0x71, 0x36, 0x13, 0x7f, 0xea, 0xba, 0x0b,
  0x3e, 0xbd, 0xef, 0x7c, 0x34, 0x41, 0xe1, 0xb7, 0x99, 0x46, 0x9f, 0x81,
  0xc6, 0xcf, 0x72, 0x94, 0x65, 0x52, 0xf0, 0x88, 0x17, 0xf6, 0x00, 0xf4,
  0xd0, 0x9a, 0x66, 0xe7, 0x1e, 0xa5, 0xfd, 0x4b, 0x95, 0xf3, 0x0e, 0x0f,
  0x10, 0x60, 0x74, 0x54, 0xda, 0xb0, 0x91, 0x0a, 0x10, 0x07, 0x19, 0x30,
  0x71, 0x34, 0x5f, 0xe2, 0x1c, 0x08, 0x77, 0x2e, 0xbc, 0xa3, 0x13, 0x18,
  0x22, 0x5e, 0xf9, 0xe0, 0x67, 0x8e, 0xbc, 0xc0, 0x7d, 0x02, 0x03, 0x01,
  0x00, 0x01, 0xa3, 0x7b, 0x30, 0x79, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d,
  0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x2c, 0x06, 0x09, 0x60, 0x86, 0x48,
  0x01, 0x86, 0xf8, 0x42, 0x01, 0x0d, 0x04, 0x1f, 0x16, 0x1d, 0x4f, 0x70,
  0x65, 0x6e, 0x53, 0x53, 0x4c, 0x20, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61,
  0x74, 0x65, 0x64, 0x20, 0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63,
  0x61, 0x74, 0x65, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16,
  0x04, 0x14, 0x5c, 0xac, 0x85, 0x36, 0xd2, 0x6c, 0x5b, 0x71, 0xd0, 0x7e,
  0x78, 0xe6, 0x4b, 0xc7, 0x9e, 0x0b, 0x06, 0xaa, 0x70, 0x8b, 0x30, 0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b, 0x13, 0x60, 0xb4, 0x80, 0x57, 0xbe, 0xe3, 0x25, 0xb6, 0x9a,
  0x75, 0x95, 0x3d, 0xba, 0xe5, 0x48, 0xa6, 0x3b, 0x1c, 0xd7, 0x50, 0x90,
  0x19, 0x4c, 0xf5, 0x22, 0x94, 0x7f, 0x79, 0xd5, 0x5e, 0x5a, 0xfb, 0x29,
  0x34, 0xf8, 0xd7, 0xf5, 0x2e, 0x95, 0x81, 0xb7, 0x2c, 0x8f, 0x2f, 0xcf,
  0xdd, 0xc4, 0xcf, 0x1b, 0x2e, 0x2b, 0xe8, 0x4d, 0x92, 0xf6, 0x8f, 0xba,
  0x08, 0x1a, 0x8a, 0x13, 0x2c, 0x2d, 0x38, 0xce, 0xfe, 0xa5, 0x59, 0xc5,
  0xea, 0x3a, 0x07, 0x8b, 0xf9, 0x8d, 0x33, 0x25, 0xc0, 0x4e, 0x75, 0x51,
  0xfc, 0xd1, 0x12, 0xec, 0x85, 0xb1, 0xb5, 0xf4, 0xc2, 0xb8, 0x05, 0x3c,
  0x00, 0x4a, 0x1b, 0xab, 0xb2, 0xab, 0x69, 0x58, 0x33, 0x93, 0xf8, 0x47,
  0x12, 0x75, 0xc2, 0x3a, 0x82, 0xe1, 0x2d, 0x95, 0xa9, 0x9c, 0xb9, 0xe3,
  0x3d, 0x29, 0x93, 0x65, 0xa1, 0x79, 0xee
};
static unsigned int test_device_cert_der_len = 991;

static unsigned char test_device_cert_key_der[] = {
  0x30, 0x82, 0x04, 0xa3, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
  0xc4, 0x7e, 0xd4, 0xe3, 0xa2, 0x0c, 0x78, 0xe6, 0x0f, 0xa5, 0x3c, 0x01,
  0x9b, 0x01, 0xdf, 0x96, 0xf6, 0x8e, 0xa0, 0x80, 0x4b, 0xb1, 0xc7, 0x20,
  0x3c, 0xca, 0x81, 0x9a, 0xce, 0x08, 0x5f, 0x70, 0x99, 0x8d, 0x44, 0x46,
  0xb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0x4b, 0x95, 0xf3, 0x0e, 0x0f, 0x10, 0x60, 0x74, 0x54, 0xda,
  0xb0, 0x91, 0x0a, 0x10, 0x07, 0x19, 0x30, 0x71, 0x34, 0x5f, 0xe2, 0x1c,
  0x08, 0x77, 0x2e, 0xbc, 0xa3, 0x13, 0x18, 0x22, 0x5e, 0xf9, 0xe0, 0x67,
  0x8e, 0xbc, 0xc0, 0x7d, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01,
  0x00, 0x20, 0x0c, 0x53, 0xaa, 0xa0, 0xe6, 0x89, 0xdd, 0x74, 0x15, 0xce,
  0x18, 0xb0, 0xf0, 0x78, 0x48, 0x46, 0x04, 0x73, 0x37, 0x19, 0xa1, 0x70,
  0x49, 0xd8, 0xc0, 0x6a, 0xad, 0x16, 0x07, 0x95, 0x91, 0x66, 0xe9, 0xfc,
  0x85, 0xd1, 0x11, 0xfd, 0x96, 0x5d, 0xd7, 0x87, 0xd2, 0x7b, 0xd9, 0xb2,
  0x14, 0xfc, 0xb3, 0xb3, 0x51, 0x32, 0xc0, 0x84, 0xf3, 0x2c, 0xd2, 0xff,
  0xef, 0x29, 0x8f, 0x35, 0x89, 0x20, 0x83, 0x6d, 0x22, 0xb5, 0x48, 0x9d,
  0xec, 0xe4, 0x20, 0x33, 0x8c, 0x5e, 0x14, 0x51, 0x52, 0xdc, 0xbd, 0x04,
  0xd1, 0x0a, 0xa0, 0xe2, 0xda, 0x4c, 0xb8, 0xa7, 0xe7, 0x0e, 0xc8, 0x29,
  0xfd, 0xa4, 0x72, 0xe2, 0xbd, 0xb6, 0x4b, 0x5c, 0x28, 0x78, 0xda, 0x09,
  0x74, 0x12, 0x0b, 0x5b, 0x53, 0x52, 0x0c, 0x01, 0x54, 0xf7, 0x69, 0x0c,
  0xf6, 0x45, 0xb9, 0x79, 0x77, 0x61, 0xfa, 0x23, 0x36, 0x8e, 0x54, 0x0f,
  0x19, 0x63, 0xd4, 0x84, 0xd7, 0xea, 0xc5, 0x3e, 0x8c, 0x4f, 0x2e, 0x1d,
  0x24, 0xd6, 0x53, 0x15, 0x47, 0xcd, 0xb6, 0x43, 0x67, 0x94, 0x85, 0x43,
  0xe1, 0x90, 0x8b, 0xef, 0x19, 0x88, 0xcf, 0xa9, 0x1c, 0x50, 0xce, 0x33,
  0x11, 0x2c, 0x61, 0x92, 0xf4, 0xc9, 0xa0, 0xa4, 0x34, 0xdc, 0x7e, 0xea,
  0xb5, 0x27, 0x0b, 0x35, 0xb0, 0x83, 0x22, 0x85, 0x60, 0x1f, 0xa0, 0xaa,
  0x46, 0x01, 0xd7, 0x60, 0x5f, 0x8f, 0x1a, 0x75, 0x8d, 0x65, 0x7e, 0xf2,
  0xbc, 0xb0, 0xf4, 0xaa, 0x45, 0x36, 0x09, 0x01, 0x3d, 0x1e, 0x6a, 0x9c,
  0x51, 0xcf, 0x71, 0xe6, 0x73, 0x4e, 0x45, 0xe0, 0x36, 0x4c, 0xe0, 0xca,
  0x79, 0x6c, 0x62, 0x32, 0xca, 0xfd, 0x61, 0xb2, 0x3b, 0x88, 0xc7, 0x9c,
  0x7a, 0x5f, 0x87, 0x66, 0x6e, 0x04, 0xcc, 0x71, 0x50, 0x2e, 0x21, 0x37,
  0x2e, 0x33, 0xef, 0x5e, 0x9d, 0x02, 0x81, 0x81, 0x00, 0xf2, 0x58, 0xc2,
  0xc5, 0x96, 0xec, 0x49, 0x0c, 0x8b, 0x4f, 0x60, 0x8b, 0x31, 0xd7, 0x66,
  0xe3, 0x7e, 0x0e, 0xd1, 0xce, 0x93, 0x89, 0xea, 0x34, 0xd3, 0x93, 0xa2,
  0xed, 0xfb, 0x38, 0xa9, 0xc7, 0x99, 0x5b, 0xae, 0x1b, 0x69, 0xd8, 0x9d,
  0x70, 0x6b, 0x91, 0x93, 0x42, 0x03, 0x7c, 0xd9, 0x90, 0xa3, 0xec, 0xa5,
  0x9a, 0xf1, 0xc4, 0xd6, 0x97, 0x64, 0x08, 0x2b, 0xe8, 0x9c, 0x7b, 0xfa,
  0xeb, 0xd3, 0x4c, 0x3c, 0x14, 0x6c, 0xc9, 0x7d, 0x0b, 0x60, 0xe2, 0x9e,
  0x08, 0x66, 0xde, 0xd2, 0xc5, 0x41, 0xd8, 0xa7, 0x26, 0x04, 0x78, 0x73,
  0x02, 0x58, 0x9f, 0xb6, 0x0c, 0x0e, 0x54, 0x06, 0x50, 0xf3, 0x45, 0xd4,
  0xf2, 0x76, 0x50, 0x95, 0xe2, 0x5c, 0xea, 0x50, 0x22, 0xe2, 0x0d, 0xec,
  0x68, 0x3f, 0x51, 0xd3, 0x18, 0xe8, 0x29, 0x30, 0x21, 0xf1, 0xfa, 0x12,
  0x7a, 0xfa, 0xc4, 0x4b, 0xa7, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x90, 0xc8,
  0x24, 0xed, 0xe9, 0x04, 0x5b, 0x4a, 0x76, 0xdc, 0xfb, 0xf8, 0x70, 0x43,
  0x0e, 0xf6, 0x9b, 0x68, 0x2c, 0x5d, 0x5b, 0x9e, 0x27, 0x80, 0x0e, 0x2d,
  0x66, 0x5f, 0x51, 0x70, 0xcb, 0xfb, 0x70, 0x25, 0x96, 0x06, 0xa5, 0x74,
  0x53, 0xaf, 0x45, 0x37, 0xbd, 0x4f, 0x22, 0xd9, 0x79, 0xef, 0xd3, 0x6d,
  0x33, 0xbe, 0xd4, 0x34, 0x82, 0x31, 0x50, 0x12, 0xab, 0x64, 0x5f, 0x45,
  0x0e, 0x90, 0xfd, 0x9c, 0x7f, 0xdb, 0xa1, 0xbe, 0x83, 0xbf, 0x50, 0x21,
  0x62, 0xb3, 0x90, 0x1c, 0x36, 0xf0, 0x44, 0x32, 0xca, 0xa9, 0xac, 0x30,
  0xcd, 0x27, 0x50, 0xf0, 0xed, 0x25, 0xf6, 0x35, 0x62, 0xd5, 0x8d, 0x8c,
  0x54, 0xfc, 0xbb, 0xd0, 0xb7, 0x53, 0x09, 0x7f, 0xed, 0x1b, 0xda, 0xcd,
  0x96, 0x2e, 0xfc, 0x73, 0xdd, 0xe6, 0x1b, 0xb2, 0x00, 0xf7, 0x4c, 0xd7,
  0xf9, 0x19, 0xdc, 0x47, 0x3b, 0x02, 0x81, 0x80, 0x2e, 0x37, 0x43, 0x1d,
  0x8f, 0x44, 0x8c, 0x41, 0xba, 0x27, 0x6f, 0x0f, 0xdc, 0x77, 0x69, 0x85,
  0xa4, 0xba, 0x8a, 0xc4, 0x4c, 0x5a, 0x96, 0xb1, 0x70, 0x4f, 0xcd, 0x58,
  0x78, 0xfa, 0x3b, 0x56, 0x77, 0x26, 0x86, 0xb3, 0x6f, 0xa2, 0x01, 0x94,
  0x38, 0xcf, 0x50, 0x37, 0xdc, 0x96, 0x68, 0x9f, 0x2e, 0xc3, 0xa8, 0x69,
  0x3e, 0x05, 0xfd, 0xae, 0x6d, 0x43, 0x29, 0xac, 0x78, 0x7b, 0x24, 0x44,
  0xe8, 0xcc, 0xb7, 0x00, 0x66, 0x26, 0x9e, 0x94, 0x16, 0xeb, 0xe0, 0x9b,
  0x55, 0x92, 0xd4, 0xbb, 0x0d, 0x6d, 0x95, 0xa2, 0xa8, 0x63, 0x97, 0xfb,
  0x30, 0x8f, 0x74, 0x96, 0x23, 0xfd, 0x5e, 0x1e, 0xfd, 0xe5, 0xa9, 0x50,
  0x7b, 0xc3, 0xae, 0x71, 0xb6, 0xaa, 0x70, 0x91, 0xd6, 0x6e, 0x35, 0x51,
  0x44, 0x12, 0x04, 0xa1, 0xd1, 0x54, 0x59, 0x7b, 0x7f, 0xbb, 0xe2, 0xde,
  0x55, 0x08, 0x2e, 0x2d, 0x02, 0x81, 0x81, 0x00, 0x9c, 0x64, 0xd6, 0xa9,
  0x1c, 0xa9, 0x8a, 0x22, 0xcf, 0x04, 0xa8, 0x95, 0x22, 0x28, 0x54, 0x1e,
  0x9e, 0x29, 0x92, 0x60, 0x7b, 0x27, 0x61, 0x0f, 0x1a, 0x29, 0x94, 0x83,
  0xa2, 0x26, 0x56, 0xfb, 0x1b, 0x44, 0xf0, 0xcd, 0x0d, 0xcc, 0xf4, 0xcd,
  0x69, 0x2e, 0x9a, 0x45, 0x05, 0xaf, 0x98, 0x75, 0x8c, 0x02, 0x9e, 0x31,
  0x73, 0x03, 0xa9, 0x5c, 0xbf, 0x93, 0x39, 0xbd, 0xf2, 0x2e, 0xe6, 0x75,
  0x87, 0x41, 0xcf, 0xfd, 0x76, 0x9b, 0x74, 0x55, 0x08, 0x7d, 0x73, 0xea,
  0x4e, 0xa9, 0xb2, 0xd8, 0xa2, 0x48, 0x80, 0x8c, 0xbb, 0x33, 0x86, 0xc1,
  0xcd, 0x71, 0xeb, 0x56, 0x84, 0x57, 0xf2, 0xf9, 0xb9, 0x36, 0xa2, 0x1a,
  0x17, 0x23, 0xdc, 0xab, 0x84, 0xe1, 0x8f, 0x86, 0x34, 0x8a, 0xb4, 0x85,
  0x08, 0x7a, 0x27, 0x43, 0x76, 0xf2, 0x3f, 0x39, 0x03, 0x1c, 0xdf, 0x4e,
  0x4d, 0xf1, 0xef, 0xa9, 0x02, 0x81, 0x80, 0x5c, 0x2d, 0x0a, 0x0d, 0x51,
  0x48, 0xcd, 0x38, 0x64, 0xf3, 0x7a, 0xfd, 0x1a, 0x19, 0x42, 0x28, 0x68,
  0xca, 0xda, 0x98, 0x2b, 0x46, 0xbb, 0x85, 0xdd, 0x55, 0x3d, 0xd8, 0x61,
  0x7d, 0xc4, 0x90, 0x9d, 0xee, 0xe8, 0xb2, 0xc8, 0x85, 0xc9, 0x43, 0x14,
  0xf7, 0x74, 0x01, 0xeb, 0x03, 0x0b, 0x7f, 0x24, 0x13, 0x99, 0x04, 0xff,
  0x95, 0xf0, 0x08, 0x45, 0x76, 0xce, 0x00, 0xe8, 0xc5, 0x76, 0x34, 0xf9,
  0x91, 0xa0, 0xc8, 0x83, 0xa9, 0x78, 0x87, 0x1d, 0xac, 0xc9, 0x66, 0x4d,
  0x90, 0x46, 0x4e, 0x99, 0xef, 0xdd, 0x16, 0xa9, 0xc9, 0x43, 0xd6, 0xbf,
  0xa1, 0x6b, 0x33, 0x8b, 0x68, 0x1d, 0x25, 0xd2, 0x95, 0x0d, 0xd0, 0x4d,
  0xe2, 0xd9, 0x1e, 0x9c, 0xe0, 0x4a, 0xad, 0x1e, 0xda, 0xfd, 0x0d, 0xc1,
  0xf7, 0x9c, 0xa7, 0x55, 0xb1, 0x70, 0xad, 0x33, 0x15, 0x1e, 0xe5, 0xc0,
  0x44, 0x78, 0xd1
};
static unsigned int test_device_cert_key_der_len = 1191;



#ifdef NX_SECURE_ENABLE_PSK_CIPHERSUITES
static UCHAR tls_psk[] = { 0x1a, 0x2b, 0x3c, 0x4d };
#endif


#define     DEMO_STACK_SIZE  4096 //  (3 * 1024 / sizeof(ULONG))

/* Define the IP thread's stack area.  */
#define IP_STACK_SIZE 4096 //(2 * 1024 / sizeof(ULONG))

/* Define packet pool for the demonstration.  */
#define NX_PACKET_POOL_BYTES  ((1536 + sizeof(NX_PACKET)) * 20)
#define NX_PACKET_POOL_SIZE (NX_PACKET_POOL_BYTES/sizeof(ULONG) + 64 / sizeof(ULONG))

/* Define the ARP cache area.  */
#define ARP_AREA_SIZE 1024 // (512 / sizeof(ULONG))

#define TOTAL_STACK_SPACE (2 * (DEMO_STACK_SIZE + IP_STACK_SIZE + NX_PACKET_POOL_SIZE + ARP_AREA_SIZE))

#ifndef __LINUX__
ULONG test_stack_area[TOTAL_STACK_SPACE + 2000];
#endif

/* Define the counters used in the demo application...  */
ULONG                   error_counter;


/* Define thread prototypes.  */

static void    ntest_0_entry(ULONG thread_input);
static void    ntest_1_entry(ULONG thread_input);
static void    ntest_0_connect_received(NX_TCP_SOCKET *server_socket, UINT port);
static void    ntest_0_disconnect_received(NX_TCP_SOCKET *server_socket);
extern void    _nx_ram_network_driver_1500(struct NX_IP_DRIVER_STRUCT *driver_req);

/* Define what the initial system looks like.  */
#ifndef __LINUX__
void    tx_application_define(void *first_unused_memory)
#else
#ifdef CTEST
void test_application_define(void *first_unused_memory);
void test_application_define(void *first_unused_memory)
#else
void           nx_secure_tls_cert_callback_fail_test_application_define(void *first_unused_memory)
#endif
#endif
{
CHAR       *pointer;
UINT       status;

    /* Setup the working pointer.  */
#ifndef __LINUX__
    pointer = (CHAR*)test_stack_area;
#else
    pointer = (CHAR *) first_unused_memory;
#endif

    error_counter = 0;
    
    /* Create the main thread.  */
    tx_thread_create(&ntest_0, "thread 0", ntest_0_entry, 0,  
                     pointer, DEMO_STACK_SIZE, 
                     3, 3, TX_NO_TIME_SLICE, TX_AUTO_START);

    pointer = pointer + DEMO_STACK_SIZE;

    /* Create the main thread.  */
    tx_thread_create(&ntest_1, "thread 1", ntest_1_entry, 0,  
                     pointer, DEMO_STACK_SIZE, 
                     4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);

    pointer = pointer + DEMO_STACK_SIZE;

    /* Initialize the NetX system.  */
    nx_system_initialize();
      
    /* Create a packet pool.  */
    status = nx_packet_pool_create(&pool_0, "NetX Main Packet Pool", 1536, pointer, NX_PACKET_POOL_BYTES);
    pointer = pointer + NX_PACKET_POOL_BYTES;

    if(status)
        error_counter++;

    /* Create an IP instance.  */
    status = nx_ip_create(&ip_0, "NetX IP Instance 0", IP_ADDRESS(1, 2, 3, 4), 0xFFFFFF00UL, &pool_0, _nx_ram_network_driver_1500,
                          pointer, IP_STACK_SIZE, 1);
    pointer = pointer + IP_STACK_SIZE;

    /* Create another IP instance.  */
    status += nx_ip_create(&ip_1, "NetX IP Instance 1", IP_ADDRESS(1, 2, 3, 5), 0xFFFFFF00UL, &pool_0, _nx_ram_network_driver_1500,
                           pointer, IP_STACK_SIZE, 1);
    pointer = pointer + IP_STACK_SIZE;

    if(status)
        error_counter++;

    /* Enable ARP and supply ARP cache memory for IP Instance 0.  */
    status = nx_arp_enable(&ip_0, (void *) pointer, ARP_AREA_SIZE);
    pointer = pointer + ARP_AREA_SIZE;

    /* Enable ARP and supply ARP cache memory for IP Instance 1.  */
    status += nx_arp_enable(&ip_1, (void *) pointer, ARP_AREA_SIZE);
    pointer = pointer + ARP_AREA_SIZE;

    /* Check ARP enable status.  */
    if(status)
        error_counter++;

    /* Enable TCP processing for both IP instances.  */
    status = nx_tcp_enable(&ip_0);
    status += nx_tcp_enable(&ip_1);

    /* Check TCP enable status.  */
    if(status)
        error_counter++;
    
    nx_secure_tls_initialize();
}

/*  Define callbacks used by TLS.  */
/* Include CRL associated with Verisign root CA (for AWS) for demo purposes. */
#include "test_ca.crl.der.c"


/* Timestamp function - should return Unix time formatted 32-bit integer. */
static ULONG tls_timestamp_function(void)
{
    // Return a fixed epoch - 1500939067 seconds = 07/24/2017 @ 11:31pm (UTC) 
    return(0x5976833BL); 
}

/* Callback invoked whenever TLS has to validate a certificate from a remote host. Additional checking
   of the certificate may be done by the application here. */
static ULONG certificate_verification_callback(NX_SECURE_TLS_SESSION *session, NX_SECURE_X509_CERT* certificate)
{
    return(233);
}


/* Define the test threads.  */

/* -----===== SERVER =====----- */

static CHAR *html_data =  "HTTP/1.1 200 OK\r\n" \
        "Date: Fri, 15 Sep 2016 23:59:59 GMT\r\n" \
        "Content-Type: text/html\r\n" \
        "Content-Length: 200\r\n\r\n" \
        "<html>\r\n"\
        "<body>\r\n"\
        "<b>Hello NetX Secure User!</b>\r\n"\
        "This is a simple webpage\r\n"\
        "served up using NetX Secure!\r\n"\
        "</body>\r\n"\
        "</html>\r\n";

static void    ntest_0_entry(ULONG thread_input)
{
UINT       status;
ULONG      actual_status;
NX_PACKET *send_packet;
NX_PACKET *receive_packet;
UCHAR receive_buffer[100];
ULONG bytes;

    /* Print out test information banner.  */                                     
    printf("NetX Secure Test:   TLS Cert Callback Fail Test........................");

    /* Check for earlier error.  */
    if(error_counter)
    {
        printf("ERROR!\n");
        test_control_return(1);
    }

    /* Ensure the IP instance has been initialized.  */
    status = nx_ip_status_check(&ip_0, NX_IP_INITIALIZE_DONE, &actual_status, NX_IP_PERIODIC_RATE);

    /* Check for error.  */
    if(status)
        error_counter++;

    /* Create a socket.  */
    status = nx_tcp_socket_create(&ip_0, &server_socket, "Server Socket", 
                                  NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE * 100, 16*1024,
                                  NX_NULL, ntest_0_disconnect_received);

    /* Check for error.  */
    if(status)
        error_counter++;

    /* Create a TLS session for our socket.  */
    status =  nx_secure_tls_session_create(&server_tls_session,
                                           &nx_crypto_tls_ciphers,
                                           server_crypto_metadata,
                                           sizeof(server_crypto_metadata));

    /* Check for error.  */
    if(status)
        error_counter++;

    /* Setup our packet reassembly buffer. */
    nx_secure_tls_session_packet_buffer_set(&server_tls_session, server_packet_buffer, sizeof(server_packet_buffer));

    /* Enable Client Certificate Verification. */
    nx_secure_tls_session_client_verify_enable(&server_tls_session);

    /* Add a timestamp function for time checking and timestamps in the TLS handshake. */
    _nx_secure_tls_session_time_function_set(&server_tls_session, tls_timestamp_function);

    /* Setup the callback invoked when TLS has a certificate it wants to verify so we can
       do additional checks not done automatically by TLS. */
    _nx_secure_tls_session_certificate_callback_set(&server_tls_session, certificate_verification_callback);
    

    /////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Initialize our certificate
    nx_secure_x509_certificate_initialize(&certificate, test_device_cert_der, test_device_cert_der_len, NX_NULL, 0, test_device_cert_key_der, test_device_cert_key_der_len, NX_SECURE_X509_KEY_TYPE_RSA_PKCS1_DER);
    nx_secure_tls_local_certificate_add(&server_tls_session, &certificate);

    nx_secure_x509_certificate_initialize(&ica_certificate, ica_cert_der, ica_cert_der_len, NX_NULL, 0, NULL, 0, NX_SECURE_X509_KEY_TYPE_NONE);
    nx_secure_tls_local_certificate_add(&server_tls_session, &ica_certificate);

    // If we are testing client certificate verify, allocate remote certificate space.
    nx_secure_tls_remote_certificate_allocate(&server_tls_session, &client_remote_certificate, client_remote_cert_buffer, sizeof(client_remote_cert_buffer));
    nx_secure_tls_remote_certificate_allocate(&server_tls_session, &client_remote_issuer, client_remote_issuer_buffer, sizeof(client_remote_issuer_buffer));

    /* Add a CA Certificate to our trusted store for verifying incoming client certificates. */
    nx_secure_x509_certificate_initialize(&trusted_certificate, ca_cert_der, ca_cert_der_len, NX_NULL, 0, NULL, 0, NX_SECURE_X509_KEY_TYPE_NONE);
    nx_secure_tls_trusted_certificate_add(&server_tls_session, &trusted_certificate);

    /////////////////////////////////////////////////////////////////////////////////////////////////////////


    /* Setup this thread to listen.  */
    status = nx_tcp_server_socket_listen(&ip_0, 12, &server_socket, 5, ntest_0_connect_received);

    /* Check for error.  */
    if(status)
        error_counter++;

#ifdef NX_SECURE_ENABLE_PSK_CIPHERSUITES
          /* For PSK ciphersuites, add a PSK and identity hint.  */
        nx_secure_tls_psk_add(&server_tls_session, tls_psk, sizeof(tls_psk), "Client_identity", 15, "12345678", 8);
#endif

    /* Accept a client socket connection.  */
    status = nx_tcp_server_socket_accept(&server_socket, NX_IP_PERIODIC_RATE);
    
    //tx_thread_suspend(&ntest_0);    
    

    /* Check for error.  */
    if(status)
        error_counter++;

    /* Start the TLS Session now that we have a connected socket. */
    /* Certificate callback failed. */
    status = nx_secure_tls_session_start(&server_tls_session, &server_socket, NX_WAIT_FOREVER);
    EXPECT_EQ(NX_SECURE_TLS_ALERT_RECEIVED, status);

    /* Disconnect the server socket.  */
    status = nx_tcp_socket_disconnect(&server_socket, NX_IP_PERIODIC_RATE);

    /* Check for error.  */
    if(status)
        error_counter++;

    /* Unaccept the server socket.  */
    status = nx_tcp_server_socket_unaccept(&server_socket);

    /* Check for error.  */
    if(status)
        error_counter++;

    /* Unlisten on the server port.  */
    status = nx_tcp_server_socket_unlisten(&ip_0, 12);

    /* Check for error.  */
    if (status)
        error_counter++;

    /* Delete the socket.  */
    status = nx_tcp_socket_delete(&server_socket);

    /* Check for error.  */
    if(status)
        error_counter++;
}



/* -----===== CLIENT =====----- */
static void    ntest_1_entry(ULONG thread_input)
{
UINT         status;
NX_PACKET *send_packet;
NX_PACKET *receive_packet;
UCHAR receive_buffer[400];
ULONG bytes;

    /* Create a socket.  */
    status = nx_tcp_socket_create(&ip_1, &client_socket, "Client Socket", 
                                  NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE * 100, 1024*16,
                                  NX_NULL, NX_NULL);

    /* Check for error.  */
    if(status)
        error_counter++;

    /* Create a TLS session for our socket.  */
    status =  nx_secure_tls_session_create(&client_tls_session,
                                           &nx_crypto_tls_ciphers,
                                           client_crypto_metadata,
                                           sizeof(client_crypto_metadata));

    /* Check for error.  */
    if(status)
        error_counter++;


    /* Setup our packet reassembly buffer. */
    nx_secure_tls_session_packet_buffer_set(&client_tls_session, client_packet_buffer, sizeof(client_packet_buffer));

    /* Make sure client certificate verification is disabled. */
    nx_secure_tls_session_client_verify_disable(&client_tls_session);

    /* Bind the socket.  */
    status = nx_tcp_client_socket_bind(&client_socket, 12, NX_IP_PERIODIC_RATE);

    /* Check for error.  */
    if(status)
        error_counter++;

    /* Need to allocate space for the certificate coming in from the remote host. */
    nx_secure_tls_remote_certificate_allocate(&client_tls_session, &remote_certificate, remote_cert_buffer, sizeof(remote_cert_buffer));
    nx_secure_tls_remote_certificate_allocate(&client_tls_session, &remote_issuer, remote_issuer_buffer, sizeof(remote_issuer_buffer));

    //nx_secure_x509_certificate_initialize(&certificate, cert_der, cert_der_len, NX_NULL, 0, private_key_der, private_key_der_len, NX_SECURE_X509_KEY_TYPE_RSA_PKCS1_DER);
/*    nx_secure_x509_certificate_initialize(&client_certificate, test_device_cert_der, test_device_cert_der_len, NX_NULL, 0, test_device_cert_key_der, test_device_cert_key_der_len, NX_SECURE_X509_KEY_TYPE_RSA_PKCS1_DER);
    nx_secure_tls_local_certificate_add(&client_tls_session, &client_certificate); */

    /* Add a CA Certificate to our trusted store for verifying incoming server certificates. */
    nx_secure_x509_certificate_initialize(&trusted_certificate, ca_cert_der, ca_cert_der_len, NX_NULL, 0, NULL, 0, NX_SECURE_X509_KEY_TYPE_NONE);
    nx_secure_tls_trusted_certificate_add(&client_tls_session, &trusted_certificate);

#ifdef NX_SECURE_ENABLE_PSK_CIPHERSUITES
    /* For PSK ciphersuites, add a PSK and identity hint.  For the client, we need to add the identity
       and set it for the particular server with which we want to communicate. 
       "Client_identity" is the identity hint used by default in the OpenSSL s_server application
       when uisng PSK ciphersuites. */       
    nx_secure_tls_psk_add(&client_tls_session, tls_psk, sizeof(tls_psk), "Client_identity", 15, "12345678", 8);
    
    /* Our target server will use this PSK entry. */
    nx_secure_tls_client_psk_set(&client_tls_session, tls_psk, sizeof(tls_psk), "Client_identity", 15, "12345678", 8);
#endif 


    /* Add a timestamp function for time checking and timestamps in the TLS handshake. */
    _nx_secure_tls_session_time_function_set(&client_tls_session, tls_timestamp_function);

    /* Setup the callback invoked when TLS has a certificate it wants to verify so we can
       do additional checks not done automatically by TLS. */
    _nx_secure_tls_session_certificate_callback_set(&client_tls_session, certificate_verification_callback);
    

    status = nx_tcp_client_socket_connect(&client_socket, IP_ADDRESS(1, 2, 3, 4), 12, 5 * NX_IP_PERIODIC_RATE);

    if(status)
        error_counter++;

    //tx_thread_resume(&ntest_0); 
    
    nx_secure_tls_session_start(&client_tls_session, &client_socket, NX_WAIT_FOREVER);

    /* End the TLS session. This is required to properly shut down the TLS connection. */
    status = nx_secure_tls_session_end(&client_tls_session, NX_WAIT_FOREVER);

    /* If the session did not shut down cleanly, this is a possible security issue. */
    if (status)
    {
        printf("Error in TLS Client session end: %x\n", status);
        error_counter++;
    }

    /* Disconnect this socket.  */
    status = nx_tcp_socket_disconnect(&client_socket, NX_IP_PERIODIC_RATE);

    /* Check for error.  */
    if(status)
        error_counter++;

    /* Unbind the socket.  */
    status = nx_tcp_client_socket_unbind(&client_socket);

    /* Check for error.  */
    if(status)
        error_counter++;

    /* Delete the socket.  */
    status = nx_tcp_socket_delete(&client_socket);

    /* Check for error.  */
    if(status)
        error_counter++;

    /* Determine if the test was successful.  */
    if(error_counter)
    {
        printf("ERROR!\n");
        test_control_return(1);
    }
    else
    {
        printf("SUCCESS!\n");
        test_control_return(0);
    }
}

static void    ntest_0_connect_received(NX_TCP_SOCKET *socket_ptr, UINT port)
{

    /* Check for the proper socket and port.  */
    if((socket_ptr != &server_socket) || (port != 12))
        error_counter++;
}

static void    ntest_0_disconnect_received(NX_TCP_SOCKET *socket)
{

    /* Check for proper disconnected socket.  */
    if(socket != &server_socket)
        error_counter++;
}
#else
#ifdef CTEST
void test_application_define(void *first_unused_memory);
void test_application_define(void *first_unused_memory)
#else
void           nx_secure_tls_cert_callback_fail_test_application_define(void *first_unused_memory)
#endif
{

    /* Print out test information banner.  */
    printf("NetX Secure Test:   TLS Cert Callback Fail Test........................N/A\n");
    test_control_return(3);
}
#endif
